/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Message.CalendarItemInfo;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mime.MimeVisitor;
/*     */ import com.zimbra.cs.mime.MimeVisitor.VisitPhase;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Part;
/*     */ import javax.mail.internet.ContentType;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ForwardAppointmentInvite
/*     */   extends ForwardAppointment
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  58 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  59 */     Account senderAcct = getZDesktopSafeAuthenticatedAccount(zsc);
/*  60 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  61 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*     */ 
/*     */ 
/*  65 */     ItemId iid = new ItemId(request.getAttribute("id"), zsc);
/*  66 */     if (!iid.belongsTo(getRequestedAccount(zsc)))
/*     */     {
/*  68 */       return proxyRequest(request, context, iid.getAccountId());
/*     */     }
/*     */     
/*  71 */     Element msgElem = request.getElement("m");
/*  72 */     ParseMimeMessage.MimeMessageData parsedMessageData = new ParseMimeMessage.MimeMessageData();
/*  73 */     MimeMessage mmFwdWrapper = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, null, ParseMimeMessage.NO_INV_ALLOWED_PARSER, parsedMessageData);
/*     */     
/*     */ 
/*     */ 
/*  77 */     Message msg = mbox.getMessageById(octxt, iid.getId());
/*  78 */     if (msg == null) {
/*  79 */       throw MailServiceException.NO_SUCH_MSG(iid.getId());
/*     */     }
/*     */     
/*  82 */     Pair<MimeMessage, MimeMessage> msgPair = getMessagePair(mbox, senderAcct, msg, mmFwdWrapper);
/*  83 */     forwardMessages(mbox, octxt, msgPair);
/*     */     
/*  85 */     Element response = getResponseElement(zsc);
/*  86 */     return response;
/*     */   }
/*     */   
/*     */   public static void forwardMessages(Mailbox mbox, OperationContext octxt, Pair<MimeMessage, MimeMessage> msgPair) throws ServiceException {
/*  90 */     if (msgPair.getFirst() != null) {
/*  91 */       sendFwdMsg(octxt, mbox, (MimeMessage)msgPair.getFirst());
/*     */     }
/*  93 */     if (msgPair.getSecond() != null) {
/*  94 */       sendFwdNotifyMsg(octxt, mbox, (MimeMessage)msgPair.getSecond());
/*     */     }
/*     */   }
/*     */   
/*     */   public static Pair<MimeMessage, MimeMessage> getMessagePair(Mailbox mbox, Account senderAcct, Message msg, MimeMessage mmFwdWrapper) throws ServiceException
/*     */   {
/* 100 */     mbox.lock.lock();
/*     */     Pair<MimeMessage, MimeMessage> msgPair;
/* 102 */     try { MimeMessage mmInv = msg.getMimeMessage();
/* 103 */       List<Invite> invs = new ArrayList();
/* 104 */       for (Iterator<Message.CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext();) {
/* 105 */         Message.CalendarItemInfo cii = (Message.CalendarItemInfo)iter.next();
/* 106 */         Invite inv = cii.getInvite();
/* 107 */         if (inv != null) {
/* 108 */           invs.add(inv);
/*     */         }
/*     */       }
/* 111 */       ZCalendar.ZVCalendar cal = null;
/* 112 */       Invite firstInv = null;
/* 113 */       boolean first; if (!invs.isEmpty())
/*     */       {
/* 115 */         first = true;
/* 116 */         for (Invite inv : invs) {
/* 117 */           if (first) {
/* 118 */             first = false;
/* 119 */             firstInv = inv;
/* 120 */             cal = inv.newToICalendar(true);
/*     */           } else {
/* 122 */             ZCalendar.ZComponent comp = inv.newToVComponent(true, true);
/* 123 */             cal.addComponent(comp);
/*     */           }
/*     */         }
/*     */       }
/*     */       else {
/*     */         try {
/* 129 */           CalPartDetectVisitor visitor = new CalPartDetectVisitor();
/* 130 */           visitor.accept(mmInv);
/* 131 */           MimeBodyPart calPart = visitor.getCalendarPart();
/* 132 */           if (calPart != null) {
/* 133 */             String ctHdr = calPart.getContentType();
/* 134 */             ContentType ct = new ContentType(ctHdr);
/* 135 */             String charset = ct.getParameter("charset");
/* 136 */             if ((charset == null) || (charset.length() == 0))
/* 137 */               charset = "utf-8";
/* 138 */             InputStream is = calPart.getInputStream();
/*     */             try {
/* 140 */               cal = ZCalendar.ZCalendarBuilder.build(is, charset);
/*     */             } finally {
/* 142 */               ByteUtil.closeStream(is);
/*     */             }
/* 144 */             Object invList = Invite.createFromCalendar(senderAcct, msg.getFragment(), cal, false);
/* 145 */             if ((invList != null) && (!((List)invList).isEmpty()))
/* 146 */               firstInv = (Invite)((List)invList).get(0);
/* 147 */             if (firstInv == null)
/* 148 */               throw ServiceException.FAILURE("Error building Invite for calendar part in message " + msg.getId(), null);
/*     */           }
/*     */         } catch (MessagingException e) {
/* 151 */           throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
/*     */         } catch (IOException e) {
/* 153 */           throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
/*     */         }
/*     */       }
/*     */       
/* 157 */       msgPair = getInstanceFwdMsg(senderAcct, firstInv, cal, mmInv, mmFwdWrapper);
/*     */     }
/*     */     finally {
/* 160 */       mbox.lock.release();
/*     */     }
/* 162 */     return msgPair;
/*     */   }
/*     */   
/*     */ 
/*     */   private static class CalPartDetectVisitor
/*     */     extends MimeVisitor
/*     */   {
/*     */     private MimeBodyPart mCalPart;
/*     */     
/*     */ 
/* 172 */     public MimeBodyPart getCalendarPart() { return this.mCalPart; }
/*     */     
/*     */     private static boolean matchingType(Part part, String ct) throws MessagingException {
/* 175 */       String mmCtStr = part.getContentType();
/* 176 */       if (mmCtStr != null) {
/* 177 */         ContentType mmCt = new ContentType(mmCtStr);
/* 178 */         return mmCt.match(ct);
/*     */       }
/* 180 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitBodyPart(MimeBodyPart bp) throws MessagingException
/*     */     {
/* 185 */       if ((this.mCalPart == null) && (matchingType(bp, "text/calendar")))
/* 186 */         this.mCalPart = bp;
/* 187 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitMessage(MimeMessage mm, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 192 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitMultipart(MimeMultipart mp, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 197 */       return false;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ForwardAppointmentInvite.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */